﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Core.Utils;
using S4Store.Service;
using S4Store.Domain;
using LX.Web.Mvc;
using LX.Core.Cryptography;
using S4Store.Models.AdministratorModels;

namespace S4Store.Controllers
{
    public class AdministratorController : BaseController
    {

        public StoreService StoreService { get; set; }
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Query()
        {
            QueryModel query = new QueryModel
            {
                Page = 1,
                PageSize = 20
            };
            return Query(query);
        }

        [HttpPost]
        public ActionResult Query(QueryModel query)
        {
            using (var t = new Transtion())
            {
                t.Inject<AdministratorService>(AdministratorService);
                var querysource = AdministratorService.GetQuery();
                if (!Helper.StringIsEmpty(query.Name))
                {
                    querysource = querysource.Where(x => x.Name.Contains(query.Name));
                }
                if (!Helper.StringIsEmpty(query.UserName))
                {
                    querysource = querysource.Where(x => x.UserName.Contains(query.UserName));
                }
                if (query.Position!=null)
                {
                    querysource = querysource.Where(x => x.PositionType == query.Position.Value);
                }
                if (!Helper.StringIsEmpty(query.StoreName))
                {
                    querysource = querysource.Where(x =>x.Store!=null&& x.Store.Name.Contains(query.StoreName));
                }
                if (!Helper.StringIsEmpty(query.StoreCode))
                {
                    querysource = querysource.Where(x => x.Store != null && x.Store.Code==query.StoreCode);
                }
                if (query.Status != 0)
                {
                    if (query.Status == 1)
                    {
                        querysource = querysource.Where(x => x.IsClose == false);
                    }
                    else
                    {
                        querysource = querysource.Where(x => x.IsClose == true);
                    }
                }
                int total = querysource.Count();
                querysource = querysource.OrderByDescending(x => x.Id);
                if (query.Page > 1 && query.PageSize > 0)
                {
                    querysource = querysource.Skip((query.Page - 1) * query.PageSize);
                }
                querysource = querysource.Take(query.PageSize);
                LX.Core.Query.PagedList<Administrator> data = new LX.Core.Query.PagedList<Administrator>
                {
                    Data = querysource.ToList(),
                    Page = query.Page,
                    PageSize = query.PageSize,
                    Total = total
                };
                t.Commit();
                return View(data);
            } 
        }

        public ActionResult Add()
        {
            using (var t = new Transtion())
            {
                t.Inject<StoreService>(StoreService);
                var stores = StoreService.GetQuery().ToList();
                t.Commit();
                return View(stores);
            } 
        }

        [HttpPost]
        public JsonResult Add(Administrator user)
        {
            using (var t = new Transtion())
            {
                t.Inject<AdministratorService>(AdministratorService);
                AdministratorService.AddAdministrator(user);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success=true
                    }
                };
            } 
        }

        [HttpPost]
        public ActionResult Modify(int id)
        {
            using (var t = new Transtion())
            {
                t.Inject<AdministratorService>(AdministratorService);
                t.Inject<StoreService>(StoreService);
                var user = AdministratorService.GetQuery().FirstOrDefault(x => x.Id == id);
                var stores = StoreService.GetQuery().ToList();
                t.Commit();
                if (user != null)
                {
                    Combin<List<Store>, Administrator> model = new Combin<List<Store>, Administrator>
                    {
                        Data1 = stores,
                        Data2 = user
                    };
                    return View(model);
                }
                return this.Content("用户不存在");
            }            
        }


        [HttpPost]
        public JsonResult ModifyBack(Administrator user)
        {
            using (var t = new Transtion())
            {
                t.Inject<AdministratorService>(AdministratorService);
                AdministratorService.ModifyAdministrator(user);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }


        [HttpPost]
        public JsonResult Close(List<int> ids)
        {
            if (Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要关闭的帐号");
            }
            using (var t = new Transtion())
            {
                t.Inject<AdministratorService>(AdministratorService);
                foreach (var id in ids)
                {
                    var user= AdministratorService.GetQuery().FirstOrDefault(x=>x.Id==id);
                    if (user != null)
                    {
                        user.IsClose = true;
                    }
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "已关闭"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        [HttpPost]
        public JsonResult Open(List<int> ids)
        {
            if (Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要开启的帐号");
            }
            using (var t = new Transtion())
            {
                t.Inject<AdministratorService>(AdministratorService);
                foreach (var id in ids)
                {
                    var user = AdministratorService.GetQuery().FirstOrDefault(x => x.Id == id);
                    if (user != null)
                    {
                        user.IsClose = false;
                    }
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "已开启"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        public ActionResult ModifyPassword()
        {
            return View();
        }
        [HttpPost]
        public JsonResult ModifyPasswordBack(String newpassword1, String newpassword2, String oldpassword)
        {
            using (var t = new Transtion())
            {
                t.Inject<AdministratorService>(AdministratorService);
                if(CurrentUser.Password!= MD5.Encrypt(oldpassword)){
                    throw new Exception("原密码错误");
                }
                if (string.IsNullOrWhiteSpace(newpassword1))
                {
                    throw new Exception("请填写新密码");
                }
                if (newpassword1 != newpassword2)
                {
                    throw new Exception("两次密码输入不一致");
                }
                var user = AdministratorService.GetQuery().FirstOrDefault(x => x.Id == CurrentUser.Id);
                user.Password = MD5.Encrypt(newpassword1);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }
    }
}
